package com.davidtorma.site;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class GalleryFilterServiceImpl implements GalleryFilterService {
	private List<GalleryFilter> mainFilters = new ArrayList<GalleryFilter>();
	private List<GalleryFilter> subFilters = new ArrayList<GalleryFilter>();

	public GalleryFilterServiceImpl() {
		initGalleryFilters();
	}

	public List<GalleryFilter> getGalleryMainFilters() {
		return mainFilters;
	}

	public List<GalleryFilter> getGallerySubFilters() {
		return subFilters;
	}

	private void initGalleryFilters() {
		File iconRoot = new File("e:\\git\\davidtorma-web\\davidtorma.web\\src\\main\\webapp\\" + Constants.ICON_PATH);
		addFilter(iconRoot, mainFilters);
		File[] subCategories = iconRoot.listFiles(directoryFilter);
		for (File sub : subCategories) {
			addFilter(sub, subFilters);
		}
	}

	private static FileFilter directoryFilter = new FileFilter() {
		public boolean accept(File file) {
			return file.isDirectory();
		}
	};

	private void addFilter(File dir, List<GalleryFilter> filters) {
		File[] files = dir.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				GalleryFilter galleryFilter = new GalleryFilter(file.getName(), file.getName(), file.getName());
				filters.add(galleryFilter);
			}
		}
	}
}
